package group2.controller;

import group2.model.ReceiveAddress;
import group2.model.UserInfo;
import group2.service.IReceiveAddressService;
import group2.utils.JWTUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 薛献军
 * @date 2020/8/25 19:42
 */
@Controller
@RequestMapping("/address")
public class ReceiveAddressController {

    @Autowired
    IReceiveAddressService receiveAddressService;

    /**
     * 添加地址
     * @param receiveAddress
     * @param def
     * @return
     */
    @GetMapping("/add")
    @ResponseBody
     public Map<String ,Object> addReceAddress(ReceiveAddress receiveAddress, @RequestParam("checked") String def, HttpServletRequest request){
         Map<String,Object> map=new HashMap<>();
        String token = request.getHeader("token");
        //获取用户编号
        String userId = JWTUtil.getTokenInfo(token).getClaim("userId").asString();
          if (def.equals("是")){
              //不是前后端测试要改成具体的用户
              int defAddressId = this.receiveAddressService.selectDefault(userId);
              this.receiveAddressService.updateAddressNo(defAddressId);
              this.receiveAddressService.addAddressYes(receiveAddress,userId);
          }else {
              this.receiveAddressService.addAddressNo(receiveAddress,userId);
          }
          map.put("state",true);
          map.put("msg","成功添加收货地址");
          return map;
     }

    /**
     * 更改为默认地址
     */
    @GetMapping("/update")
    @ResponseBody
    public Map<String ,Object> updateDefaultAddress(@RequestParam("addressId") int addressId, HttpServletRequest request){
          Map<String,Object> map=new HashMap<>();
        String token = request.getHeader("token");
        //获取用户编号
        String userId = JWTUtil.getTokenInfo(token).getClaim("userId").asString();
          int defAddressId = this.receiveAddressService.selectDefault(userId);
          this.receiveAddressService.updateAddressNo(defAddressId);
          this.receiveAddressService.updateAddressYes(addressId,userId);
          map.put("msg","成功更改为默认收货地址");
          return map;
    }

    /**
     * 删除地址
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String ,Object> deleteAddress(@RequestParam("addressId") int addressId,HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        String token = request.getHeader("token");
        this.receiveAddressService.delectAddress(addressId);
        map.put("msg","成功删除收货地址");
        return map;
    }

    /**
     * 修改收货地址信息
     */
    @RequestMapping("/updatDef")
    @ResponseBody
    public Map<String,Object> updateAddress(ReceiveAddress receiveAddress,HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        String token = request.getHeader("token");
        this.receiveAddressService.updateAddress(receiveAddress);
        map.put("state",true);
        map.put("msg","成功更改地址信息");
        return map;
    }


}
